package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.*;

import cn.com.util.Tools;
import cn.com.util.*;
import cn.com.util.*;


public class T_user_adminBaseController {

	private static Logger logger = Logger.getLogger(T_user_adminBaseController.class);
	
	@Autowired
	private ApiT_user_adminService apiT_user_adminService;
	@Autowired
	private ApiT_user_roleService apiT_user_roleService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "t_user_admin-list")
	public ModelAndView t_user_admin_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_admin_list.jsp");
		
		List<ApiT_user_admin> t_user_admins = apiT_user_adminService.findT_user_admins();
		mv.addObject("t_user_admins", t_user_admins);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "t_user_admin-add-pre")
	public ModelAndView t_user_admin_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_user_admin_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_user_admin t_user_admin = apiT_user_adminService.findT_user_adminById(Integer.valueOf(id));
			mv.addObject("t_user_admin",t_user_admin);
		}
		
		List<ApiT_user_role> roles = apiT_user_roleService.findT_user_roles();
		mv.addObject("roles",roles);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys",citys);
		
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "t_user_admin-add")
	public @ResponseBody Map<String,Object> t_user_admin_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		String true_name = request.getParameter("true_name");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String add_time = request.getParameter("add_time");
		String login_count = request.getParameter("login_count");
		String last_login = request.getParameter("last_login");
		String last_ip = request.getParameter("last_ip");
		String action_list = request.getParameter("action_list");
		String city_id = request.getParameter("city_id");
		String role_id = request.getParameter("role_id");
		String status = request.getParameter("status");
		String is_super = request.getParameter("is_super");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		int roleId = Integer.parseInt(role_id);
		ApiT_user_role userRole = apiT_user_roleService.findT_user_roleById(roleId);
		//add
		if(id==null || (id.trim()).equals("")){
			
			ApiT_user_admin t_user_admin = new ApiT_user_admin();
			
			//添加管理员的角色为分站站长
			if (Integer.parseInt(userRole.getRole_type()) == 1){
				int cityId = Integer.parseInt(city_id);
				if (cityId == -1){//选的是总站
					map.put(Utils.RESULT, "-1");
					map.put(Utils.MESSAGE, "你所添加的管理员为站长角色,请选择具体分站！");
					return map;
				}
				
				List<ApiT_user_admin> admins = apiT_user_adminService.getUserAdminByCityAndRole(cityId,roleId);
				if (admins.size() > 0){//已经存在分站站长
					map.put(Utils.RESULT, "-2");
					map.put(Utils.MESSAGE, "你所选择分站已经存在站长！");
					return map;
				}
			}
          
		    //t_user_admin.setUser_id(user_id);
		    t_user_admin.setUser_name(user_name);
		    t_user_admin.setTrue_name(true_name);
		    //t_user_admin.setEmail(email);
		    password = Tools.toMD5(Tools.RmFilter(password));
		    t_user_admin.setPassword(password);
		    t_user_admin.setAdd_time(add_time);
		    t_user_admin.setLogin_count(0);
		    //t_user_admin.setLast_login(last_login);
		    //t_user_admin.setLast_ip(last_ip);
		    //t_user_admin.setAction_list(action_list);
		    t_user_admin.setCity_id(city_id);
		    t_user_admin.setRole_id(Integer.valueOf(role_id));
		    t_user_admin.setStatus(0);
		    t_user_admin.setIs_super(Integer.valueOf(is_super));
	       
			apiT_user_adminService.insertT_user_admin(t_user_admin);
		}
		//edit
		else{
			ApiT_user_admin t_user_admin = apiT_user_adminService.findT_user_adminById(Integer.valueOf(id));
	      
			//修改管理员的角色为分站站长
			if (Integer.parseInt(userRole.getRole_type()) == 1){
				int cityId = Integer.parseInt(city_id);
				if (cityId == -1){//选的是总站
					map.put(Utils.RESULT, "-1");
					map.put(Utils.MESSAGE, "你所添加的管理员为站长角色,请选择具体分站！");
					return map;
				}
				
				List<ApiT_user_admin> admins = apiT_user_adminService.getUserAdminByCityAndRole(cityId,roleId);
				if (admins.size() > 0){//已经存在分站站长
					for (ApiT_user_admin admin : admins) {
						if (admin.getUser_id().intValue() != Integer.parseInt(id)) {//这个分站已经有其它的站长
							map.put(Utils.RESULT, "-2");
							map.put(Utils.MESSAGE, "你所选择分站已经存在其它站长！");
							return map;
						}
					}
				}
			}
			
		    //t_user_admin.setUser_id(user_id);
		    t_user_admin.setUser_name(user_name);
		    t_user_admin.setTrue_name(true_name);
		    //t_user_admin.setEmail(email);
		    if (password.equals(t_user_admin.getPassword())){//没有修改密码
		    	
		    }else{
		    	 password = Tools.toMD5(Tools.RmFilter(password));
				 t_user_admin.setPassword(password);
		    }
		    t_user_admin.setAdd_time(add_time);
		    //t_user_admin.setLast_login(last_login);
		    //t_user_admin.setLast_ip(last_ip);
		    //t_user_admin.setAction_list(action_list);
		    t_user_admin.setCity_id(city_id);
		    t_user_admin.setRole_id(Integer.valueOf(role_id));
		    t_user_admin.setIs_super(Integer.valueOf(is_super));
		    
			apiT_user_adminService.updateT_user_admin(t_user_admin);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		return map;
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "t_user_admin-delete")
	public void t_user_admin_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiT_user_adminService.deleteT_user_admin(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



